package com.web.base.user.util;

import com.blue.integration.serve.Bucket;
import com.blue.integration.serve.StorageClient;
import com.blue.jdbc.Page;
import com.web.base.user.message.BankCardRequest;
import com.web.base.user.message.BankCardResponse;
import com.web.base.user.model.BankCard;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 银行卡转换器
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2018年06月26日
 */
public class BankCardConvertor
{
	private BankCardConvertor()
	{
	}
	
	public static BankCard toBankCard(BankCardRequest request)
	{
		if (request == null)
			return null;
		
		BankCard bankCard = new BankCard();
		bankCard.setId(request.getId());
		bankCard.setUserId(request.getUserId());
		bankCard.setBankId(request.getBankId());
		bankCard.setFacePic(request.getFacePic());
		bankCard.setBackPic(request.getBackPic());
		bankCard.setOwner(request.getOwner());
		bankCard.setMobile(request.getMobile());
		bankCard.setBankNo(request.getBankNo());
		bankCard.setExpireDate(request.getExpireDate());
		bankCard.setState(request.getState());
		bankCard.setAuth(request.getAuth());
		return bankCard;
	}
	
	public static List<BankCard> toBankCard(List<BankCardRequest> requestList)
	{
		List<BankCard> list = new ArrayList<>();
		if (requestList == null || requestList.isEmpty())
			return list;
		
		for (BankCardRequest request : requestList)
		{
			list.add(toBankCard(request));
		}
		return list;
	}
	
	public static BankCardResponse toResponse(BankCard bankCard, StorageClient storageClient)
	{
		if (bankCard == null)
			return null;
		
		BankCardResponse response = new BankCardResponse();
		response.setId(bankCard.getId());
		response.setUserId(bankCard.getUserId());
		response.setBankId(bankCard.getBankId());
		response.setFacePic(bankCard.getFacePic());
		response.setBackPic(bankCard.getBackPic());
		response.setOwner(bankCard.getOwner());
		response.setMobile(bankCard.getMobile());
		response.setBankNo(bankCard.getBankNo());
		response.setExpireDate(bankCard.getExpireDate());
		response.setState(bankCard.getState());
		response.setAuth(bankCard.getAuth());

		response.setFacePicUrl(storageClient.getUrl(Bucket.FILE, bankCard.getFacePic()));
		response.setBackPicUrl(storageClient.getUrl(Bucket.FILE, bankCard.getBackPic()));
		return response;
	}
	
	public static List<BankCardResponse> toResponse(List<BankCard> list, StorageClient storageClient)
	{
		List<BankCardResponse> responseList = new ArrayList<>();
		if (list == null || list.isEmpty())
			return responseList;
		
		for (BankCard bankCard : list)
		{
			responseList.add(toResponse(bankCard, storageClient));
		}
		return responseList;
	}
	
	public static Map<Integer, BankCardResponse> toResponse(Map<Integer, BankCard> map, StorageClient storageClient)
	{
		Map<Integer, BankCardResponse> responseMap = new HashMap<>();
		if (map == null || map.isEmpty())
			return responseMap;
		
		for (Map.Entry<Integer, BankCard> entry : map.entrySet())
		{
			responseMap.put(entry.getKey(), toResponse(entry.getValue(), storageClient));
		}
		return responseMap;
	}
	
	public static Page<BankCardResponse> toResponse(Page<BankCard> page, StorageClient storageClient)
	{
		if (page == null)
			return null;
		
		Page<BankCardResponse> responsePage = new Page<>();
		responsePage.setCurPage(page.getCurPage());
		responsePage.setRows(page.getRows());
		responsePage.setSize(page.getSize());
		responsePage.setStartRowNo(page.getStartRowNo());
		responsePage.setTotalPage(page.getTotalPage());
		responsePage.setObjectList(toResponse(page.getObjectList(), storageClient));
		return responsePage;
	}
	
}
